package com.huanwei.login_regist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.huanwei.mini.R;
import com.huanwei.mini.http.API;
import com.huanwei.mini.http.Zhuce;
import com.huanwei.mini.utils.JSONUtils;
import com.huanwei.mini.utils.Okhttp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册页面
 */
public class RegistActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.lr_sr_sjh)
    EditText etPhone;
    @BindView(R.id.lr_sr_mm)
    EditText etMm;
    @BindView(R.id.lr_cxsr_mm)
    EditText etQrmm;
    @BindView(R.id.but_regist)
    Button butRegist;

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.xingming)
    EditText xingming;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.but_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_regist:
                if (TextUtils.isEmpty(name.getText().toString())) {
                    Toast.makeText(this, "请输入电话号码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    Toast.makeText(this, "请输入身份证号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(etMm.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(etQrmm.getText().toString())) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (etQrmm.getText().toString().length() < 6  ) {
                    Toast.makeText(this, "密码太短，请重新输入！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(xingming.getText().toString())) {
                    Toast.makeText(this, "请输入真实姓名", Toast.LENGTH_SHORT).show();
                    return;
                }

                API.zhuce(name.getText().toString(), etMm.getText().toString(), etMm.getText().toString(), etQrmm.getText().toString(),xingming.getText().toString(), new Okhttp.Objectcallback() {
                    @Override
                    public void onsuccess(String st) {
                        Log.e("11111", "xx"+st);
                        Zhuce zhuce = JSONUtils.parseJSON(st, Zhuce.class);
                        if (zhuce.getError()==0) {
                            Toast.makeText(RegistActivity.this, "注册成功！" +zhuce.getMsg(), Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(RegistActivity.this, "注册失败，"+zhuce.getMsg() , Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onFalia(int code, String errst) {
                        Toast.makeText(RegistActivity.this, "网络错误"+errst, Toast.LENGTH_SHORT).show();

                    }
                });


                break;
        }
    }
}
